package com.sikadwafastdaisfondturbo.pretloan.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.sikadwafastdaisfondturbo.pretloan.R
import com.sikadwafastdaisfondturbo.pretloan.app.App
import com.sikadwafastdaisfondturbo.pretloan.databinding.DialogAppUpdateBinding
import com.sikadwafastdaisfondturbo.pretloan.databinding.DialogAppUpdateItemBinding
import com.sikadwafastdaisfondturbo.pretloan.pro.ProDialog
import com.sikadwafastdaisfondturbo.pretloan.utils.DensityUtils
import com.sikadwafastdaisfondturbo.pretloan.utils.event.EventType
import com.sikadwafastdaisfondturbo.pretloan.utils.helper.SikadwaKefuConfigHelper
import com.sikadwafastdaisfondturbo.pretloan.utils.http.HttpHelper

/**
 * Author : wwdeng
 * Date : 2023/8/1 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 版本更新提示dialog
 */
class SikadwaUpdateDialog: ProDialog {
    private lateinit var binding: DialogAppUpdateBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    override fun initWindow() {
        val params = window?.attributes
        params?.width = App.getPhoneWidth() - DensityUtils.dp2px(48f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogAppUpdateBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        val showKey = "30s"
        val showText = String.format(context.getString(R.string.update_take_time),showKey)
        val index = showText.indexOf(showKey)
        val stringSpannable = SpannableString(showText)
        stringSpannable.setSpan(ForegroundColorSpan(Color.parseColor("#FFFBB24C")), index, index + showKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        //加粗
        stringSpannable.setSpan(StyleSpan(Typeface.BOLD), index, index + showKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.textUpdateTakeTime.text = stringSpannable


        val versionData = SikadwaKefuConfigHelper.takeSikadwaUpdateData()
        versionData?.let {
            binding.textVersionName.text = "V${it.optString("versionName")}"
            //添加更新描述
            addVersionUpdateInfoItem(it.optString("versionRemark"))

            //是否强制更新
            val isBreak = SikadwaKefuConfigHelper.isSikadwaShowUpdateBreak()
            if (isBreak){
                binding.btnCancel.visibility = View.GONE
                setCancelable(false)
                setCanceledOnTouchOutside(false)
            } else {
                binding.btnCancel.visibility = View.VISIBLE
                setCancelable(true)
                setCanceledOnTouchOutside(true)
            }
        }
    }

    override fun initListener() {
        setOnClickListener(R.id.btnUpdate)
        setOnClickListener(R.id.btnCancel)
    }

    override fun initData() {
    }


    /**
     * 添加更新描述
     */
    private fun addVersionUpdateInfoItem(info: String){
        binding.versionInfoLayout.removeAllViews()
        if (!info.contains("$")){
            val itemBinding = DialogAppUpdateItemBinding.inflate(layoutInflater)
            itemBinding.textVersionInfo.text = info
            binding.versionInfoLayout.addView(itemBinding.root)
            return
        }
        val list = info.split("$")
        list.forEach {
            val itemBinding = DialogAppUpdateItemBinding.inflate(layoutInflater)
            itemBinding.textVersionInfo.text = it
            binding.versionInfoLayout.addView(itemBinding.root)
        }
    }

    override fun onViewClick(v: View?) {
        when(v?.id){
            R.id.btnCancel -> {
                HttpHelper.addEvent(EventType.CLICK.CLICK_UPDATE_CLOSE,eventRemark)
            }
            R.id.btnUpdate -> {
                HttpHelper.addEvent(EventType.CLICK.CLICK_UPDATE_NOW,eventRemark)
                mCallback?.onUpdateNowClick()
            }
        }
        dismiss()
    }

    interface IDialogCallback {
        //按钮点击
        fun onUpdateNowClick()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_APP_UPDATE_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_APP_UPDATE_DISMISS,eventRemark)
    }
}